package com.wolfalpha.utron.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.bean.ItemUActivity;
import com.wolfalpha.utron.utils.StringUtils;

import java.util.List;

/**
 * 活动列表适配器
 * <p/>
 * Created by luodon on 15/9/5.
 */
public class ItemActivityAdapter extends BaseAdapter {


    private LayoutInflater inflater;
    private Context context = null;
    private List<ItemUActivity> activities;


    public ItemActivityAdapter(Context context, List<ItemUActivity> rs) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        activities = rs;
    }

    public void setData(List<ItemUActivity> rs) {
        if (rs != null) {
            activities = rs;
            this.notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return activities.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_utron_activtiy, null);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_title);
            viewHolder.tvPtime = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_ptime);
            viewHolder.tvIntro = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_intro);
            viewHolder.tvATime = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_atime);
            viewHolder.tvAddress = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_address);
            viewHolder.tvState = (TextView) convertView.findViewById(R.id.tv_item_utron_activity_state);
            viewHolder.ivPic = (ImageView) convertView.findViewById(R.id.iv_item_utron_activity_pic);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (activities != null) {
            //设置控件内容
            viewHolder.tvTitle.setText(activities.get(position).getTitle());
            viewHolder.tvPtime.setText(StringUtils.friendly_time(StringUtils.TimeStamp2Date(activities.get(position).getTime() * 1000)));
            viewHolder.tvIntro.setText(activities.get(position).getIntro());
            viewHolder.tvATime.setText("活动时间: " + activities.get(position).getActivity_time());
            viewHolder.tvAddress.setText("活动地点: " + activities.get(position).getActivity_address());
            int state = activities.get(position).getActivity_state();
            viewHolder.tvState.setText(state == 0 ? "报名中" : (state == 1 ? "正在进行" : "已结束"));
            Picasso.with(context)
                    .load(activities.get(position).getPic())
                    .placeholder(R.mipmap.project_default)
                    .into(viewHolder.ivPic);
        }
        return convertView;
    }

    /**
     * 控件
     */
    class ViewHolder {
        private TextView tvTitle;        //标题
        private TextView tvPtime;      //发布时间
        private TextView tvATime;        //活动时间
        private TextView tvIntro;       //简介
        private TextView tvAddress;    //地点
        private TextView tvState;         //活动状态
        private ImageView ivPic;         //图片


    }
}
